import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:wayofdressing/dio/configureDio.dart';
import 'package:wayofdressing/all_information/user_information.dart';
import 'package:wayofdressing/dio/UserDetailDio.dart';

class ChooseGenderSheet extends StatefulWidget {
  @override
  State<ChooseGenderSheet> createState() => _ChooseGenderSheetState();
}

class _ChooseGenderSheetState extends State<ChooseGenderSheet> {
  String _selectedGender = '男';
  final List<String> _genders = ['男', '女', '不展示'];

  Future<void> upgradeGender() async {
    await updateUserDetailByUserId(
        dio: dio,
        userId: userId,
        gender: _selectedGender == '不展示' ? ' ' : _selectedGender);

    Response response = await getUserDetailByUserId(userId: userId, dio: dio);
    dynamic data = response.data['data'];
    setState(() {
      gender = data['gender'] ?? '无';
    });
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 280,
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              TextButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: Text(
                  '取消',
                  style: TextStyle(color: Colors.blueAccent, fontSize: 14),
                ),
              ),
              TextButton(
                onPressed: () {
                  upgradeGender();
                },
                child: Text(
                  '确定',
                  style: TextStyle(color: Colors.redAccent, fontSize: 14),
                ),
              ),
            ],
          ),
          Expanded(
            child: CupertinoPicker(
              itemExtent: 32.0,
              onSelectedItemChanged: (index) {
                setState(() {
                  _selectedGender = _genders[index];
                });
              },
              children: _genders
                  .map(
                    (gender) => Text(
                      gender,
                      style: TextStyle(fontSize: 24),
                    ),
                  )
                  .toList(),
            ),
          ),
        ],
      ),
    );
  }
}
